package com.eat.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.eat.constant.MessageConstant;
import com.eat.constant.StatusConstant;
import com.eat.dto.DishDTO;
import com.eat.dto.DishPageQueryDTO;
import com.eat.entity.Dish;
import com.eat.entity.DishFlavor;
import com.eat.exception.DeletionNotAllowedException;
import com.eat.mapper.DishFlavorMapper;
import com.eat.mapper.DishMapper;
import com.eat.result.PageResult;
import com.eat.service.DishService;
import com.eat.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
//        Long a=new Long();
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);

        Long id=dish.getId();
        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        if(dishFlavors!=null){
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(id);
            }
            dishFlavorMapper.insertBatch(dishFlavors);
        }



    }

    @Override
    public PageResult page(DishPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        Page<DishVO> page = dishMapper.page(dto);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        for (Long id : ids) {
            Dish dish= dishMapper.getById(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> list = dishMapper.getSetmealIdsByDishIds(ids);
        if(list !=null && list.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }



//        for (Long id : ids) {
//
//            dishMapper.deleteById(id);
//
//            dishFlavorMapper.deleteByDishId(id);
//
//        }
        dishMapper.deleteByIds(ids);

        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getById(Long id) {

        Dish dish = dishMapper.getById(id);

        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

        DishVO dishVO =new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {

        Dish dish =new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        if(dishFlavors!=null && dishFlavors.size()>0){
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(dishFlavors);
        }
    }
    public List<DishVO> listWithFlavor(DishPageQueryDTO dto) {

        List<DishVO> dishList = dishMapper.page(dto);

        List<DishVO> dishVOList = new ArrayList<>();

        for (DishVO d : dishList) {


            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            d.setFlavors(flavors);
            dishVOList.add(d);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> getDishById(Long id) {

        List<Dish> dishes = dishMapper.getDishById(id);

        return dishes;
    }
}
